<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.engine.hikvision.utils.HikVisionApiUtil" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.UUID" %>
<%@ page import="com.mzlion.core.lang.CollectionUtils" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %><%
    out.println("CountCanteenDataJob start.");
    try {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
        String querySql = "select b.*, c.lastname, c.workcode, c.departmentid, SUBCOMPANYID1 from (" +
                "  select a.xm, a.ny, sum(CAST(a.zccs AS INT)) as allZccs, sum(CAST(a.wccss AS INT)) as allWccss, " +
                "    sum(CAST(a.wccs AS INT)) as allWccs, sum(CAST(a.xycs AS INT)) as allXycs FROM \n" +
                "     (select xm, FORMAT(CAST(CONVERT(DATE, dkrq) AS DATE), 'yyyy-MM') AS ny, zccs, wccss, wccs, xycs from uf_stdkxx WHERE dkrq is not null) a group by a.xm, a.ny " +
                " ) b left join hrmresource c on b.xm = c.id";
        out.println("querySql:" +querySql);
        rs.execute(querySql);
        while (rs.next()) {
            String xm = rs.getString("xm");
            String ny = rs.getString("ny");
            if (StringUtils.isEmpty(xm) ||StringUtils.isEmpty(ny)) {
                continue;
            }
            String allZccs = rs.getString("allZccs");
            String allWccss = rs.getString("allWccss");
            String allWccs = rs.getString("allWccs");
            String allXycs = rs.getString("allXycs");
            String workcode = rs.getString("workcode");
            String bm = rs.getString("departmentid");
            String fb = rs.getString("SUBCOMPANYID1");
            String lastname = rs.getString("lastname");

            String nowDate = sdfDate.format(new Date());
            String nowTime = sdfTime.format(new Date());

            int id = 0;
            rs1.executeQuery("SELECT id FROM uf_stsccshzxx WHERE yg = ? AND yf = ?", xm, ny);
            if (rs1.next()) {
                id = rs1.getInt("id");
            }
            if (id > 0) {
                rs1.executeUpdate("UPDATE uf_stsccshzxx SET bm = ?, fb = ?, zccs = ?, wccs = ?, " +
                        "ywcss = ?, xycs = ? WHERE id = ?", bm, fb, allZccs, allWccss, allWccs, allXycs, id);
            } else {
                rs1.executeUpdate("INSERT INTO uf_stsccshzxx (yg, ygxm, gh, bm, fb, yf, zccs, wccs, ywccs, xycs, " +
                                "formmodeid, MODEUUID, modedatacreater,modedatacreatertype, modedatacreatedate, " +
                                "modedatacreatetime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", xm, lastname, workcode, bm, fb,
                        ny, allZccs, allWccss, allWccs, allXycs, "17", UUID.randomUUID().toString(), "1", "0", nowDate, nowTime);
            }
        }

        // 权限重构
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight(true);
        modeRightInfo.editModeDataShare(1, 17, 24);
    } catch (Exception e) {
        out.println("CountCanteenDataJob error:" + e);
    }
    out.println("CountCanteenDataJob end.");
%>